package com.deer.wms.base.system.web.bill;

import com.deer.wms.base.system.model.bill.*;
import com.deer.wms.base.system.service.bill.IMaterialDetailService;
import com.deer.wms.base.system.service.bill.IMaterialInfoService;
import com.deer.wms.common.core.controller.BaseController;
import com.deer.wms.common.core.domain.AjaxResult;
import com.deer.wms.common.core.page.TableDataInfo;
import com.deer.wms.common.core.result.Result;
import com.deer.wms.common.core.result.ResultGenerator;
import com.deer.wms.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 入库单 信息操作处理
 *
 * @author guo
 * @date 2019-05-13
 */
@Controller
@RequestMapping("/in/MaterialInfo")
public class MaterialController extends BaseController {

    private String prefix = "in/MaterialInfo";
    @Autowired
    private IMaterialInfoService iMaterialInfoService;

    @Autowired
    private IMaterialDetailService iMaterialDetailService;

    @Autowired
    private RedisTemplate redisTemplate;



    /**
     * 查询入库单列表
     */
    @RequiresPermissions("in:MaterialInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MaterialInfoCriteria materialInfoCriteria) {
        startPage();
        List<MaterialInfo> list = iMaterialInfoService.selectMaterialList(materialInfoCriteria);
        return getDataTable(list);
    }

    /**
     * 查询整托信息
     */
    @PostMapping("/findList")
    @ResponseBody
    @Transactional
    public TableDataInfo findList(MaterialInfoCriteria materialInfoCriteria) {
        startPage();
        List<MaterialInfo> list = iMaterialInfoService.findList(materialInfoCriteria);
        return getDataTable(list);
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String billId){


        return null;
    }

    /**
     * 查询托号列表，下拉选使用，只显示托号
     * @author 申紫微
     * @date 15:56 2021/7/4
     * @params materialInfoCriteria
     * @return com.deer.wms.common.core.page.TableDataInfo
     **/
    @GetMapping("/findListForPullDown")
    @ResponseBody
    public Result findListForPullDown(MaterialInfoCriteria materialInfoCriteria){
        startPage();
        List<MaterialInfo> list = iMaterialInfoService.findListForPullDown(materialInfoCriteria);
        return ResultGenerator.genBillSuccessResult(list);
    }

    /**
     *晶棒托入库，查看详情时，查询托盘信息
     * @author 李帅辰
     * @date 2021/6/9 14:37
     * @return com.deer.wms.common.core.page.TableDataInfo
     */
    @PostMapping("/findByMaterialBatchId")
    @ResponseBody
    @Transactional
    public TableDataInfo findByMaterialBatchId(String carrierId) {
        startPage();
        List<MaterialInfo> list = iMaterialInfoService.findByMaterialBatchId(carrierId);
        return getDataTable(list);
    }





    @GetMapping("/page")
    public String MaterialInfo() {
        return prefix + "/MaterialInfo";
    }


    @GetMapping("/detail")
    public String billInDetail()
    {
        return prefix + "/detail";
    }


    /***
   *导出
   * @author 李帅辰
   * @date 2020/9/24 11:06
   * @return com.deer.wms.common.core.domain.AjaxResult
   */
    @RequiresPermissions("in:MaterialInfo:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MaterialInfoCriteria criteria) {
        List<MaterialInfo> list = iMaterialInfoService.findList(criteria);
        ExcelUtil<MaterialInfo> util = new ExcelUtil<MaterialInfo>(MaterialInfo.class);
        return util.exportExcel(list, "晶棒托入库报表");
    }
    /**
     * 导出列表
     */
    @RequiresPermissions("in:MaterialInfo:partExcel")
    @PostMapping("/partExcel")
    @ResponseBody
    public AjaxResult partExcel(@RequestParam String ids) {
        List<MaterialInfo> byIds=new ArrayList();
        String[] split = ids.split(",");
        for (String s:split
        ) {
            MaterialInfoCriteria b=new MaterialInfoCriteria ();
            b.setCarrierId(s);
            List<MaterialInfo> list = iMaterialInfoService.selectMaterialList(b);
            for (MaterialInfo bs:list
            ) {
                byIds.add(bs);
            }




        }
        // List<BillInSapDto> billInSapList = billInSapService.findBillInSapDtoList(billInSapCriteria);
        ExcelUtil<MaterialInfo> util = new ExcelUtil<MaterialInfo>(MaterialInfo.class);
        return util.exportExcel(byIds, "晶棒托入库报表");
    }


    /**
     * 新增保存入库单
     */
//    @RequiresPermissions("in:materialInfo:add")
//    @Log(title = "入库单", businessType = BusinessType.INSERT)
//    @PostMapping("/add")
//    @ResponseBody
//    public Result addSave(materialInfo materialInfo) {
//        materialInfo.setCreateUserName(ShiroUtils.getLoginName());
//        materialInfo.setCreateUserId(ShiroUtils.getUserId());
//        iMaterialInfoService.save(materialInfo);
//  //      iMaterialInfoService.insertmaterialInfo(materialInfo);
////        BillInDetail billInDetail = new BillInDetail();
////        billInDetail.setBillId(materialInfo.getBillId());
////        billInDetailService.saveBillInDatails(billInDetail);
//        return ResultGenerator.genSuccessResult();
//    }



    /**
     * 新增入库单
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }


}
